/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.gui;

import java.awt.Font;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;

// The GUITree is a class that contains data and behavior that
// is shared by both the ASTTree and the SolutionTree.

abstract class GUITree
{
    // if childName is null, findChildByName will return the first child, if there is one.
    DefaultMutableTreeNode findChildByName (DefaultMutableTreeNode parent, String childName)
    {
	int childCount = parent.getChildCount();
	DefaultMutableTreeNode retVal = null;
	DefaultMutableTreeNode child;

	for (int cntr = 0; cntr < childCount; cntr++) {
	    child = (DefaultMutableTreeNode)parent.getChildAt(cntr);
	    
	    if (childName == null || child.toString().equals(childName)) {
		retVal = child;
		break;
	    }
	}

	return retVal;
    }

    // This method will be overriden by classes (such as SolutionTree) which
    // need to dynamically generate tree nodes.

    abstract void expandNode (AlloyGUITreeNode node);
    
    void saveTreeMode (JTree tree, java.util.List modeList, DefaultMutableTreeNode rootNode)
    {
	Enumeration expandedNodesEnumeration;
	String[] nodeNames;
	int pathLength;

	modeList.clear ();

	expandedNodesEnumeration = tree.getExpandedDescendants (new TreePath(rootNode.getPath()));
	if (expandedNodesEnumeration == null)
	    return;
	
	// We will store the path of each expanded node into our modeList,
	// representing each path as an array of Strings.
	
	while (expandedNodesEnumeration.hasMoreElements()) {
	    TreePath path = (TreePath) expandedNodesEnumeration.nextElement ();

	    if (tree.isVisible (path)) {
		pathLength = path.getPathCount ();

		nodeNames = new String[pathLength];

		// The first two items of the tree path are always "root" and "Solutions",
		// so we can ignore them. I'll store them anyway, just to be safe.
		for (int cntr = 0; cntr < pathLength; cntr++) {
		    nodeNames[cntr] = new String ( ((DefaultMutableTreeNode)path.getPathComponent(cntr)).toString() );
		    //AlloyGUI.debugPrint ("->" + nodeNames[cntr]);
		}
		//AlloyGUI.debugPrint ("\n");

		modeList.add (nodeNames);
	    }
	}
	//AlloyGUI.debugPrint ("\n");
    }

    void restoreTreeMode (JTree tree, java.util.List modeList, DefaultMutableTreeNode rootNode, boolean useExpandNode)
    {
	int pathNum, currentPathIndex;
	int vecLength;
	String[] pathNames;
	DefaultMutableTreeNode currentNode, childNode;
	ArrayList currentPath;

	vecLength = modeList.size ();

	for (pathNum = 0; pathNum < vecLength; pathNum++) {
	    pathNames = (String[])modeList.get (pathNum);
	    /*
	    for (int cntr = 0; cntr < pathNames.length; cntr++) {
		AlloyGUI.debugPrint ("->" + pathNames[cntr]);
	    }
	    AlloyGUI.debugPrint ("\n");
	    */
	    currentPathIndex = 0;

	    currentPath = new ArrayList ();
	    currentNode = rootNode;
	    currentPath.add (rootNode);
	    currentPathIndex++;

	    for (int cntr = currentPathIndex; cntr < pathNames.length; cntr++) {
		childNode = findChildByName (currentNode, pathNames[cntr]);
		if (childNode == null)
		    break;

		currentPath.add (childNode);
		if (useExpandNode)
		    expandNode ((AlloyGUITreeNode)childNode);
		currentNode = childNode;
	    }

	    // Make any of the last node's children visible, to expand it.
	    childNode = findChildByName (currentNode, null);
	    if (childNode != null)
		currentPath.add (childNode);

	    tree.makeVisible (new TreePath (currentPath.toArray()));
	}

	//AlloyGUI.debugPrint ("\n");
    }

    abstract public void clearTree ();

    // returns an array of the JTrees represented by this object
    abstract JTree[] getTrees ();

    public void setFont (Font newFont) {
	JTree[] trees = getTrees ();
	
	for (int cntr = 0; cntr < trees.length; cntr++) {
	    DefaultTreeCellRenderer renderer =
		(DefaultTreeCellRenderer)trees[cntr].getCellRenderer();
	    renderer.setFont (newFont);
	    
	    // Now we invalidate the cached row height for each tree.
	    int rowHeight = trees[cntr].getRowHeight();
	    if (rowHeight <= 0)
		trees[cntr].setRowHeight (rowHeight - 1);
	    
	    trees[cntr].repaint();
	}
    }
}
